package com.example.fillthemapwithtubes.db;

import java.io.Serializable;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class MapEntity implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4160508897097322522L;

	private static final int NO_ID = -1;

	private int id;
	private int row;
	private int column;
	private String name;
	private long createTime;
	private long updateTime;
	private String info;
	private String description;

	public MapEntity(int id, int row, int column, String name, long createTime, long updateTime, String info,
			String description) {
		this.id = id;
		this.row = row;
		this.column = column;
		this.name = name;
		this.createTime = createTime;
		this.updateTime = updateTime;
		this.info = info;
		this.description = description;
	}

	public MapEntity(int row, int column, String name, long createTime, long updateTime, String info,
			String description) {
		this(NO_ID, row, column, name, updateTime, updateTime, info, description);
	}

	public boolean isNoId() {
		return id == NO_ID;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public long getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(long updateTime) {
		this.updateTime = updateTime;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public boolean createOrUpdateMap(DBHelper dbHelper) {
		long time = System.currentTimeMillis();
		ContentValues cv = new ContentValues();
		cv.put(DBConstants.MAP_ROW, row);
		cv.put(DBConstants.MAP_COLUMN, column);
		cv.put(DBConstants.MAP_NAME, name);
		cv.put(DBConstants.MAP_UPDATE_TIME, time);
		cv.put(DBConstants.MAP_INFO, info);
		cv.put(DBConstants.MAP_DESCRIPTION, description);

		long ret = 0;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if (this.isNoId()) {
			cv.put(DBConstants.MAP_CREATE_TIME, time);
			ret = db.insert(DBConstants.TABLE_MAP, null, cv);
		} else {
			ret = db.update(DBConstants.TABLE_MAP, cv, DBConstants.PKID + " =? ", new String[] { id + "" });
		}
		db.releaseReference();
		db.close();

		return ret > 0;
	}
}
